package com.core.family.admin.controller;

import com.core.common.annotation.MenuDesc;
import com.core.common.manger.storage.IStorageService;
import com.core.family.admin.domain.po.SysStorage;
import com.core.family.admin.domain.req.SysStorageReq;
import com.core.common.domain.response.R;
import com.core.common.gen.BaseQuery;
import com.core.common.gen.PageQuery;
import com.core.family.admin.service.SysStorageService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;


/**
 * @author honglei
 * @version 1.0
 * @time: 2022-6-20 10:02:03
 * @Description 服务实现层 文件存储
 */
@Deprecated
@MenuDesc(desc = "文件存储", code = "sysStorage")
@Api(tags = "SysStorage 文件存储")
@RestController
@RequestMapping("api/SysStorage/")
public class SysStorageController {

    @Autowired
    SysStorageService service;


    /**
     * 根据id获取详情
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping(value = "get/{id}")
    public R get(HttpServletRequest request, @PathVariable("id") Long id) {
        return R.ok(service.getById(id));
    }


    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping(value = "getPage", method = {RequestMethod.GET, RequestMethod.POST})
    public R getPage(@RequestBody PageQuery<SysStorageReq> pageQuery) {
        SysStorage bean = new SysStorage();
        if (Objects.nonNull(pageQuery.getQueryBean())) {
            BeanUtils.copyProperties(pageQuery.getQueryBean(), bean);
        }
        PageQuery<BaseQuery> baseQuery = new PageQuery<>();
        baseQuery.setQueryBean(pageQuery.getQueryBean());
        baseQuery.setPageIndex(pageQuery.getPageIndex());
        baseQuery.setPageSize(pageQuery.getPageSize());
        return R.ok(service.selectPage(bean, baseQuery));
    }

    /**
     * 批量查询
     *
     * @return
     */
    @RequestMapping(value = "getList", method = {RequestMethod.GET, RequestMethod.POST})
    public R getList(@RequestBody SysStorageReq queryReq) {
        SysStorage bean = new SysStorage();

        return R.ok(service.selectList(bean, queryReq));
    }

    /**
     * 普通更新
     *
     * @param bean
     * @return
     */
    @PostMapping(value = "update")
    public R update(@RequestBody SysStorage bean) {
        return R.ok(service.updateById(bean));
    }

    /**
     * 批量新增或更新
     *
     * @param list
     * @return
     */
    @PostMapping(value = "batch/saveOrUpdate")
    public R batchUpdate(@RequestBody List<SysStorage> list) {
        return R.ok(service.saveOrUpdateBatch(list));
    }

    /**
     * 批量变更 先删后增
     *
     * @param list
     * @return
     */
    @PostMapping(value = "batch/saveAfterDel")
    public R saveAfterDel(@RequestBody List<SysStorage> list) {

        if (CollectionUtils.isEmpty(list)) {
            return R.error("参数错误");
        }
        BaseQuery baseQuery = new BaseQuery();

        return R.ok(service.batchSaveAfterDel(list, baseQuery));
    }

    /**
     * 新增
     *
     * @param bean
     * @return
     */
    @PostMapping(value = "save")
    public R save(@RequestBody SysStorage bean) {
        return R.ok(service.save(bean));
    }

    /**
     * 删除
     *
     * @param request
     * @param id
     * @return
     */
    @PostMapping(value = "del/{id}")
    public R del(HttpServletRequest request, @PathVariable("id") Long id) {
        return R.ok(service.removeById(id));
    }


    /**
     * 批量删除 -根据ids
     *
     * @param request
     * @param ids
     * @return
     */
    @PostMapping(value = "batch/delByIds")
    public R batchDel(HttpServletRequest request, @RequestBody List<String> ids) {
        return R.ok(service.removeByIds(ids));
    }

    /**
     * 批量删除 -根据条件
     *
     * @param request
     * @param baseQuery
     * @return
     */
    @PostMapping(value = "batch/delByConditions")
    public R delByConditions(HttpServletRequest request, @RequestBody SysStorageReq baseQuery) {
        SysStorage bean = new SysStorage();
        return R.ok(service.removeByConditions(bean, baseQuery));
    }

    /**
     * 文件上传
     *
     * @param request
     * @param file
     * @return
     */
    @PostMapping(value = "/file/upload")
    public Object upload(HttpServletRequest request, MultipartFile file) {
        return new IStorageService.FileMate();
    }

    /**
     * 文件下载
     *
     * @param key
     * @return
     */
    @PostMapping(value = "/file/download")
    public R fileDownload(String key) {
        return null;
    }

}
